package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.Drugs;
import his.entity.DrugsDetailed;
import his.persistence.DrugsDetailedMapper;
import his.persistence.DrugsMapper;
import his.service.PatientSearchService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

@Service
public class PatientSearchServiceImpl implements PatientSearchService {

    @Autowired
    DrugsMapper drugsMapper;
    @Autowired
    DrugsDetailedMapper drugsDetailedMapper;
    @Override
    public CommonResponse<DrugsDetailed> get_detail(int drug_id) {
        QueryWrapper<DrugsDetailed> drugsDetailedQueryWrapper = new QueryWrapper<>();
        drugsDetailedQueryWrapper.eq("DrugsID" , drug_id);
        DrugsDetailed drugsDetailed = drugsDetailedMapper.selectOne(drugsDetailedQueryWrapper);
        return CommonResponse.createForSuccess(drugsDetailed);
    }

    @Override
    public CommonResponse<List<Drugs>> get_Drugs() {
        List<Drugs> drugsList = new ArrayList<>();
        QueryWrapper<Drugs> drugsQueryWrapper = new QueryWrapper<>();
        drugsList = drugsMapper.selectList(drugsQueryWrapper);
        return CommonResponse.createForSuccess(drugsList);
    }

    @Override
    public CommonResponse<Drugs> search_Drugs(String KeyWord) {
        QueryWrapper<Drugs> drugsQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Drugs> drugsQueryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Drugs> drugsQueryWrapper2 = new QueryWrapper<>();
        drugsQueryWrapper.eq("DrugsCode" , KeyWord);
        drugsQueryWrapper1.eq("DrugsName" , KeyWord);
        drugsQueryWrapper2.eq("ID" , KeyWord);
        Drugs drugs = drugsMapper.selectOne(drugsQueryWrapper);
        Drugs drugs1 = drugsMapper.selectOne(drugsQueryWrapper1);
        Drugs drugs2 = drugsMapper.selectOne(drugsQueryWrapper2);
        if(drugs != null){
            return CommonResponse.createForSuccess(drugs);
        }
         else if(drugs1 != null){
            return CommonResponse.createForSuccess(drugs1);
        }
        else if(drugs2 != null){
            return CommonResponse.createForSuccess(drugs2);
        }else
        return CommonResponse.createForError("未找到相关药品");
    }
}
